package PerlBot::Plugin::IrcUtils;
use strict;
use warnings;
use base 'Class::Component::Plugin';
use DateTime;
use Encode qw/encode decode/;
use Web::Scraper;
use URI;
use URI::Escape;

sub call_codepad : Hook('code') {
    my ($self, $c, $args) = @_;

    $args->{irc}->yield( privmsg => $args->{where} => $args->{who} . " : ". $self->config->{url}->{codepad}.$args->{who});
    return 0;
}

sub call_j2k : Hook('j2k') {
    my ($self, $c, $args) = @_;

    if ($args->{desc}) {
 	my $target = $args->{action} && $args->{action} eq 'privmsg' ? $args->{where} : $args->{who};
	$args->{irc}->yield( privmsg => $target => $args->{who} . " : ". $self->config->{url}->{j2k}.$args->{desc});
	return 0;
    }
}

sub call_map : Hook('map') {
    my ($self, $c, $args) = @_;

    if ($args->{desc}) {
	my $where = $args->{desc};
	if ($args->{irc}->server_name() =~ /freenode/) {
	    $where = encode("euc-kr", decode("utf-8", $args->{desc}));
	}
	my $target = $args->{action} && $args->{action} eq 'privmsg' ? $args->{where} : $args->{who};
	$args->{irc}->yield( privmsg => $target => $args->{who} . " : ". $self->config->{url}->{naver_map}.URI::Escape::uri_escape($where));
	return 0;
    }
}

sub kick : Hook('kick') {
    my ($self, $c, $args) = @_;

    if ($args->{desc}) {
	if ($args->{action} && $args->{action} eq 'privmsg') {
	    $args->{irc}->yield( kick => $args->{where} => $args->{desc} );
	} else { 
	    my ($channel, $target) = split /\s/, $args->{desc};
	    $args->{irc}->yield( kick => $channel => $target);
	}
    }

    return 0;
}

sub oper : Hook('oper') {
    my ($self, $c, $args) = @_;

    if ($args->{desc}) {
	if ($args->{action} && $args->{action} eq 'privmsg') {
	    $args->{irc}->yield( mode => $args->{where} => "+oo" => $args->{desc} );
	} else { 
	    my ($channel, $target) = split /\s/, $args->{desc};
	    $args->{irc}->yield( mode => $channel => "+oo" => $target );
	}
    }
    return 0;
}

sub blah : Hook('blah') {
    my ($self, $c, $args) = @_;

    if ($args->{desc}) {
	my @block = split /\s/, $args->{desc};
	my $channel = shift @block;
	my $message = join(" ", @block);
	$args->{irc}->yield( privmsg => $channel => $message ) if $message;
    }
    return 0;
}

sub exchange : Hook('exchange') {
    my ($self, $c, $args) = @_;

    my $currency = $args->{desc};
    
    return unless $currency =~ /(USD|JPY|EUR|CNY|GBP)/i;

    my $html = scraper {
	process '/html/body/div/div[5]/div[2]/table[3]/tr[2]/td[2]', time => "TEXT";
	process '/html/body/div/div[5]/div[2]/table[4]',
	"data" => scraper {
	    process 'tr', "column[]" => "HTML";
	};
    }->scrape(URI->new("http://bank.naver.com/index/exchange.nhn"));
    
    my $target = $args->{action} && $args->{action} eq 'privmsg' ? $args->{where} : $args->{who};
    
    return unless $html->{data}->{column}->[0];
    $args->{irc}->yield( $args->{action} => $target => $html->{time} );
    foreach my $row (@{ $html->{data}->{column} }) {
	next unless $row =~ /$currency/i;
	my @data = split /  /, $self->html_strip($row);
	foreach my $item (@{ $self->config->{exchange} }) {
	    my ($key, $val) = each %{ $item };
	    next unless $data[$key];
	    $args->{irc}->yield( $args->{action} => $target => $val. " : ". $data[$key] );
	}
    }
    return;
}

sub p3rl : Hook('p3rl') {
    my ($self, $c, $args) = @_;

    my $target = $args->{action} && $args->{action} eq 'privmsg' ? $args->{where} : $args->{who};
    
    $args->{irc}->yield( $args->{action} => $target => $self->config->{url}->{p3rl} . $args->{desc} );
    return;
}

sub html_strip {
    my ($self, $text) = @_;
    $text =~ s/<(\/)?([a-zA-Z]*)(\s?[a-zA-Z]*=[^>]*)?(\s)*(\/)?>/ /g;
    $text;
}

1;
